<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-12-28 11:33:07
 * @Last Modified time: 2023-02-01 11:22:25
 */
namespace app\common\dao\store;

use frank\base\BaseDao;
use app\common\model\store\GoodsModel;
use app\common\model\store\GoodsSkuModel;

/**
 * Class GoodsDao
 * @package app\common\dao\store
 */
class GoodsDao extends BaseDao
{
    /**
     * 设置模型名
     */
    protected function setModel(): string
    {
        return GoodsModel::class;
    }

    /**
     * 获取列表
     */
    public function getGoodsList(?array $where = [], int $page, int $limit, ?array $field = [], ?array $with = [])
    {
        return $this->search($where)->with($with)->field($field ?? ['*'])->order('id DESC')->page($page)->paginate($limit);
    }

    /**
     * 获取列表
     */
    public function getGoodsListAsWith(?array $where = [], ?int $page = -1, int $limit = 0, ?array $field = [], ?array $with = [],?array $order = [])
    {
        // 排序规则
        $sort = [];
        if(!empty($order['sortType'])){
            if ($order['sortType'] === 'all') {
                $sort = ['sort', 'id' => 'desc'];
            } elseif ($order['sortType'] === 'sales') {
                $sort = ['goods_sales' => 'desc'];
            }  elseif ($order['sortType'] === 'sales_asc') {
                $sort = ['goods_sales' => 'asc'];
            } elseif ($order['sortType'] === 'max_price') {
                $sort = ['goods_max_price' => 'desc'];
            }elseif ($order['sortType'] === 'min_price') {
                $sort = ['goods_max_price'=> 'asc'];
            }
        }else{
            $sort = ['sort'=>'desc','goods_sales'=> 'desc','goods_max_price'=> 'asc'];
        }
        $field = count($field) ? $field:['*'];
        $field[] = '(sales_initial + sales_actual) as goods_sales';
        $tableName = $this->getTable();
        // 多规格商品 最高价与最低价
        $goodsSku = new GoodsSkuModel;
        $minPriceSql = $goodsSku->field(['min(price)'])
            ->where('goods_id', 'EXP', "= `$tableName`.`id`")->buildSql();
        $maxPriceSql = $goodsSku->field(['MAX(price)'])
            ->where('goods_id', 'EXP', "= `$tableName`.`id`")->buildSql();

        $field[] = $minPriceSql.' AS goods_min_price';
        $field[] = $maxPriceSql.' AS goods_max_price';

        $query = $this->search($where)
        ->with($with)
        ->field($field)
        ->withSum(['sku'=>'goods_stock'], 'stock')
        ->withMin(['sku'=>'goods_price'], 'price')
        ->withMax(['sku'=>'market_price'], 'market_price')
        ->order($sort);
         // "( 
            //     SELECT `id`,`goods_name`,`goods_imgs`,(sales_initial + sales_actual) as goods_sales,
            //     ( 
            //         SELECT min(price) FROM `s_store_goods_sku` WHERE  ( `goods_id` = `id` ) 
            //     ) AS goods_min_price,
            //     ( 
            //         SELECT MAX(price) FROM `s_store_goods_sku` WHERE  ( `goods_id` = `id` ) 
            //     ) AS goods_max_price 
            //     FROM `s_store_goods` WHERE `s_store_goods`.`delete_time` = '0' ORDER BY `sort`,`id` DESC 
            // )


        if($page === -1){
            if($limit){
                $query = $query->limit($limit);
            }
            return $query->select();
        }
        return $query->page($page)->paginate($limit);
    }
}